package game;

import java.util.Scanner;

public class Shop {
    // 进入商店的方法
    public static void enterShop(Player player, Scanner scanner) {
        System.out.println("欢迎来到商店！当前金币：" + player.gold);
        // 定义商店里的物品，但先不打印出来
        Item healingItem = new Item("治疗药水", "healing", 50, 10);
        Item attackItem = new Item("攻击药水", "attack", 15, 20);
        Item defenseItem = new Item("防御药水", "防御", 10, 15); // 防御药水
        Item staminaItem = new Item("体力药水", "体力", 20, 15); // 体力药水
        Item dodgeItem = new Item("闪避药水", "闪避", 5, 10);  // 闪避药水
        Item intelligenceItem = new Item("智力药水", "智力", 5, 10); // 智力药水
        Item damageItem = new Item("伤害药水", "damage", 30, 15); // 伤害药水
        Item strongDamageItem = new Item("强效伤害药水", "damage", 60, 45);
        Equipment woodSword = new Equipment("木剑", "Weapon", 10, 0, 10);
        Equipment leatherArmor = new Equipment("皮甲", "Armor", 0, 5, 10);
        Equipment ironSword = new Equipment("铁剑", "Weapon", 20, 0, 30);
        Equipment chainmail = new Equipment("锁子甲", "Armor", 0, 10, 50);
        Equipment dagger = new Equipment("匕首", "Weapon", 15, 0, 20);
        Equipment leatherBoots = new Equipment("皮靴", "Armor", 0, 3, 15);


        // 定义装备数组
        Equipment[] equipments = {
                new Equipment("木剑", "Weapon", 10, 0, 10),
                new Equipment("皮甲", "Armor", 0, 5, 10),
                new Equipment("铁剑", "Weapon", 20, 0, 30),
                new Equipment("锁子甲", "Armor", 0, 10, 50),
                new Equipment("匕首", "Weapon", 15, 0, 20),
                new Equipment("皮靴", "Armor", 0, 3, 15)
        };

// 定义药水数组
        Item[] items = {
                new Item("治疗药水", "healing", 50, 10),
                new Item("攻击药水", "attack", 15, 20),
                new Item("防御药水", "防御", 10, 15),
                new Item("体力药水", "体力", 20, 15),
                new Item("闪避药水", "闪避", 5, 10),
                new Item("智力药水", "智力", 5, 10),
                new Item("伤害药水", "damage", 30, 15),
                new Item("强效伤害药水", "damage", 60, 45)
        };


        int maxItems = Math.max(equipments.length, items.length);
        int equipmentNameWidth = 20; // 根据equipments中最长的名称调整
        int itemNameWidth = 20; // 根据items中最长的名称调整
        int priceWidth = 5; // 调整为实际最大价格宽度，确保价格紧跟数字

// 调整标题与价格对齐
        System.out.println("装备" + " ".repeat(equipmentNameWidth + 3) + "价格\t\t" + "药水" + " ".repeat(itemNameWidth + equipments.length - 3) + "价格");
        for (int i = 0; i < maxItems; i++) {
            // 打印装备信息，如果索引小于装备数组长度
            if (i < equipments.length) {
                System.out.printf("%d. %-" + equipmentNameWidth + "s%-" + priceWidth + "d金币\t", (i + 1), equipments[i].name, equipments[i].price);
            } else {
                // 保持对齐，不打印装备信息时
                System.out.print(" ".repeat(equipmentNameWidth + priceWidth + 4)); // 4代表序号、点和两个空格
            }

            // 打印药水信息，如果索引小于药水数组长度
            if (i < items.length) {
                System.out.printf("%d. %-" + itemNameWidth + "s%-" + priceWidth + "d金币\n", (i + 1 + equipments.length), items[i].name, items[i].price);
            } else {
                // 保持对齐，不打印药水信息时
                System.out.println();
            }
        }

        System.out.println("请选择你想要购买的物品：");


        int shopChoice = scanner.nextInt();


        // 处理购买逻辑
        switch (shopChoice) {
            case 1:
                purchaseItem(player, woodSword);
                break;
            case 2:
                purchaseItem(player, leatherArmor);
                break;
            case 3:
                purchaseItem(player, ironSword);
                break;
            case 4:
                purchaseItem(player, chainmail);
                break;
            case 5:
                purchaseItem(player, dagger);
                break;
            case 6:
                purchaseItem(player, leatherBoots);
                break;
            case 7:
                purchaseItem(player, healingItem);
                break;
            case 8:
                purchaseItem(player, attackItem);
                break;
            case 9:
                purchaseItem(player, defenseItem);
                break;
            case 10:
                purchaseItem(player, staminaItem);
                break;
            case 11:
                purchaseItem(player, dodgeItem);
                break;
            case 12:
                purchaseItem(player, intelligenceItem);
                break;
            case 13:
                purchaseItem(player, damageItem);
            case 14:
                purchaseItem(player, strongDamageItem);
                break;
            default:
                System.out.println("无效的商店选择！");
                break;
        }
    }

    private static void purchaseItem(Player player, Item item) {
        if (player.gold >= item.price) {
            player.gold -= item.price;
            player.addToInventory(item);
            System.out.println("你购买了 " + item.name + "！");
        } else {
            System.out.println("你的金币不足，无法购买" + item.name + "！");
        }
    }

    private static void purchaseItem(Player player, Equipment equipment) {
        if (player.gold >= equipment.price) {
            player.gold -= equipment.price;
            player.equip(equipment);
            System.out.println("你购买并装备了 " + equipment.name + "！");
        } else {
            System.out.println("你的金币不足，无法购买" + equipment.name + "！");
        }
    }
}
